#!/usr/bin/env bash

function usage() {
    echo "Usage: $( basename $0 ) [ -g | -h ]"
    echo "    -g: build gpu version, otherwise cpu version"
    echo "    -d: 下载同步子模块"
    echo "    -n: dry-run，只显示要执行的命令"
    echo "    -h: print this message"
    exit 1
}

function build_onnxrtm() {
    # 开始编译
    # --enable_language_interop_ops
    #                       Enable operator implemented in language other than cpp
    # --skip_submodule_sync
    #                       Don't do a 'git submodule update'. Makes the Update
    # --build_wheel         Build Python Wheel.
    # wheel文件: build/Linux/RelWithDebInfo/dist/onnxruntime-1.15.0-cp310-cp310-linux_x86_64.whl

    echo ./build.sh\
        "--config Release"\
        "--enable_language_interop_ops"\
        "--build_shared_lib"\
        "--build_wheel"\
        "--parallel"\
        "--skip_test"\
        ${extraOptions}

    if ${dryRun}
    then
        return
    fi

    # 让用户能看清命令
    sleep 2

    ./build.sh \
        --config Release \
        --enable_language_interop_ops \
        --build_shared_lib \
        --build_wheel \
        --parallel \
        --skip_test \
        ${extraOptions}
}

dryRun=false
cudaDir="/usr/local/cuda-12.1"
# 默认build cpu version
extraOptions="--skip_submodule_sync"
while getopts "gdnh" args
do
    case "${args}" in
        g)
            # 增加选项 
            extraOptions="${extraOptions} --cuda_version 12.1"
            extraOptions="${extraOptions} --use_cuda"
            extraOptions="${extraOptions} --cudnn_home ${cudaDir}"
            extraOptions="${extraOptions} --cuda_home ${cudaDir}"
            ;;
        d)
            # 删除选项
            extraOptions=${extraOptions/--skip_submodule_sync/}
            ;;
        n)
            dryRun=true
            ;;
        *)
            usage;;
    esac
done

build_onnxrtm
